package cn.edu.java.thread.锁;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadLockDemo {
    private static final ReadWriteLock lock = new ReentrantReadWriteLock();
    private static final String sharedData = "Hello, world!";

    static class Reader implements Runnable {
        @Override
        public void run() {
            lock.readLock().lock(); // Acquire the read lock
            try {
                // Read the shared data
                System.out.println(Thread.currentThread().getName() + " is reading: " + sharedData);
                Thread.sleep(2000); // Simulate some processing time
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.readLock().unlock(); // Release the read lock
            }
        }
    }

    public static void main(String[] args) {
        // Start multiple reader threads
        for (int i = 0; i < 5; i++) {
            new Thread(new Reader()).start();
        }
    }
}

